package com.ys.service.impl;

import com.ys.dao.UserMapper;
import com.ys.entity.User;
import com.ys.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName AsyncServiceImpl
 * @Author 1628994918qq.com
 * @Date 2023/3/27 18:28
 */
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    @Override
    @Async("myExecutor")
    public void executeAsync(List<User> userList, UserMapper userMapper, CountDownLatch countDownLatch) {
        try{
            log.warn("start executeAsync");
            System.out.println(Thread.currentThread().getName());
            userMapper.insertUser(userList);
            log.warn("end executeAsync");
        }finally {
            //很关键  无论上面程序是否异常必须执行countDown，否则await无法释放
            countDownLatch.countDown();
        }
    }
}
